home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue40 / Keystrok / KeysU2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-13  |  1.5 KB  |  67 lines

  1. unit KeysU2;
  2.  
  3. interface
  4.  
  5. procedure PressKey(Key: Char);
  6. procedure ReleaseKey(Key: Char);
  7. procedure SendKeys(const Keys: String);
  8.  
  9. const
  10.   SnapShotWholeScreen: Boolean = False;
  11.  
  12. implementation
  13.  
  14. uses
  15.   Windows, Forms;
  16.  
  17. procedure PostVirtualKeyEvent(vk: Word; fUp: Boolean);
  18. var
  19.   ScanCode: Byte;
  20.   Input: TInput;
  21. const
  22.   KeyEventF_KeyDown = 0;
  23.   //This constant is defined incorrectly in Delphi 4
  24.   Input_KeyBoard  = 1;
  25.   ButtonUp: array[Boolean] of Byte = (KeyEventF_KeyDown, KeyEventF_KeyUp);
  26. begin
  27.   if vk = vk_SnapShot then
  28.     { Special processing for the PrintScreen key. }
  29.     { If scan code is set to 1 it copies entire }
  30.     { screen. If set to 0 it copies active window. }
  31.     ScanCode := Byte(SnapShotWholeScreen)
  32.   else
  33.     ScanCode := MapVirtualKey(vk, 0);
  34.   FillChar(Input, SizeOf(Input), 0);
  35.   Input.IType := Input_KeyBoard;
  36.   Input.KI.wVk := vk;
  37.   Input.KI.wScan := ScanCode;
  38.   Input.KI.dwFlags := ButtonUp[fUp];
  39.   Input.KI.time := GetTickCount;
  40.   SendInput(1, Input, SizeOf(Input))
  41. end;
  42.  
  43. procedure PressKey(Key: Char);
  44. begin
  45.   PostVirtualKeyEvent(Ord(Key), False)
  46. end;
  47.  
  48. procedure ReleaseKey(Key: Char);
  49. begin
  50.   PostVirtualKeyEvent(Ord(Key), True)
  51. end;
  52.  
  53. procedure SendKeys(const Keys: String);
  54. var
  55.   Loop: Byte;
  56. begin
  57.   for Loop := 1 to Length(Keys) do
  58.   begin
  59.     PostVirtualKeyEvent(Ord(Keys[Loop]), False); { Press key }
  60.     PostVirtualKeyEvent(Ord(Keys[Loop]), True);  { Release key }
  61.   end;
  62.   { Let the keys be processed }
  63.   Application.ProcessMessages;
  64. end;
  65.  
  66. end.
  67.